package com.gm.wholesale.module.order.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.AttributeSet;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.views.dialog.DialogUtil;
import com.gm.commonlib.views.dialog.InputDialog;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.module.good.bean.GoodBean;
import com.gm.wholesale.module.main.ui.MainActivity;
import com.gm.wholesale.module.order.presenter.MainOrderFgPresenter;

import java.util.ArrayList;
import java.util.List;

public class CreateOrderViewByVoice extends CreateOrderView {
    public ImageView mUpdateGoodNameImg;

    public String mGoodName;

    public CreateOrderViewByVoice(Context context) {
        super(context);
    }

    public CreateOrderViewByVoice(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

    }


    @Override
    public void initView(Context context) {
        super.initView(context);
        mUpdateGoodNameImg = findViewById(R.id.updateGoodNameImg);
        mUpdateGoodNameImg.setOnClickListener(view -> {
            updateGoodName(mGoodBean);
        });

    }

    private void updateGoodName(GoodBean goodBean) {

        if (goodBean != null) {
            InputDialog inputDialog1 = DialogUtil.showInputDialog(getContext(), "修改商品名称", goodBean.getName(), (inputData, inputDialog) -> {
                if (TextUtils.isEmpty(inputData)) {
                    ToastUtil.showToast("请输入商品名称");
                    return;
                }
                mGoodName = inputData;
                goodBean.setName(inputData);
                inputDialog.dismiss();
                ArrayMap<String, Object> queryData = new ArrayMap<>();
                queryData.put("unitName", goodBean.getUnitName());
                queryData.put("name", inputData);
                MainOrderFgPresenter.searchGoodByName((IBaseView) getContext(), queryData, goodBeans -> updateView(goodBeans));

            });
            ChoiceGoodTagView choiceGoodTagView = new ChoiceGoodTagView(getContext());
            MainOrderFgPresenter.searchGoodByKey(null, goodBean.getName(), goodBeans -> {
                if (goodBeans != null && !goodBeans.isEmpty()) {
                    if (goodBeans.size() == 1 && goodBean.getName().equals(goodBeans.get(0).getName())) {
                        return;
                    }
                    List<GoodBean> newGoods = new ArrayList<>();
                    for (GoodBean searchGood : goodBeans) {
                        if (!goodBean.getName().equals(searchGood.getName())) {
                            newGoods.add(searchGood);
                        }
                    }
                    if (newGoods.isEmpty()) {
                        return;
                    }
                    choiceGoodTagView.showView(newGoods, newGood -> {
                        inputDialog1.dismiss();
                        List<GoodBean> goodBeans1 = new ArrayList<>();
                        goodBeans1.add(newGood);
                        updateView(goodBeans1);
                    });
                    inputDialog1.setContentView(choiceGoodTagView);
                }
            });

        }

    }


    // todo 语音查询商品
    public void showViewByVoice(MainActivity activity, String goodName) {
        reset();
        mGoodBean = new GoodBean();
        mGoodBean.setName(goodName);
        mGoodBean.setIsVoiceCreate(true);
        mainActivity = activity;
        mGoodName = goodName;
        getGoodByName(goodName, null);

    }

    public boolean isCreateByVoice() {
        return true;
    }


    public void hidChoiceUnit() {
        mChoiceUnitTv.setVisibility(GONE);
        goodUnitTv.setVisibility(VISIBLE);
    }

    public void updateView(List<GoodBean> goodBeans) {
        mChoiceUnitTv.setVisibility(GONE);
        goodUnitTv.setVisibility(VISIBLE);
        //找到商品
        if (goodBeans.size() == 1) {
            mGoodBean = goodBeans.get(0);
            //可能是修改进来，把id赋值进去。不然在返回列表会新增
            if (!TextUtils.isEmpty(mTempGoodId)) {
                mGoodBean.setTempId(mTempGoodId);
            }
            hidUpdateGoodNameImg();
            showBaseView(mainActivity, mGoodBean);

        } else {
            //   没找到 说明新商品 隐藏 高频价格、价签价格和昨日高价
            hidRightPrice();
            if (mGoodBean == null) {
                mGoodBean = new GoodBean();
            }
            mGoodBean.setUnitName("斤");
            mGoodBean.setName(mGoodName);
            goodNameTv.setText(mGoodName);
            showUnitViewByApi();
            //没有找到商品，可以修改
            showUpdateGoodNameImg();

        }
        handelInputEdFocus();

    }


    public void showUpdateGoodNameImg() {
        mUpdateGoodNameImg.setVisibility(VISIBLE);
    }

    public void hidUpdateGoodNameImg() {
        mUpdateGoodNameImg.setVisibility(GONE);
    }


}
